<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5">
        <pointList v-on:handlePointClick="handlePointClick"></pointList>
      </el-col>

      <el-col :span="19">

        <el-table v-loading="loading" :data="pointParamList" :max-height="tabHt" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="参数" align="center" prop="paramInfo.name" />
          <el-table-column label="品牌" align="center" prop="brand" />
          <el-table-column label="型号" align="center" prop="model" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--<pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />-->

        <!-- 添加或修改点位参数关联对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="品牌" prop="brand">
              <el-input type="textarea" :rows="2" v-model="form.brand" placeholder="请输入品牌" />
            </el-form-item>
            <el-form-item label="型号" prop="model">
              <el-input type="textarea" :rows="2" v-model="form.model" placeholder="请输入型号" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>

      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { listPointParam, getPointParam, delPointParam, addPointParam, updatePointParam, exportPointParam } from "@/api/basic/pointParam";
  import pointList from "@/views/components/pointTreeList";

  export default {
    name: "PointParam",
    components: {
      pointList
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 点位参数关联表格数据
        pointParamList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 100000,
          pointId: null,
          paramId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    computed: {
      tabHt(){
        return this.$store.state.app.tableHeight.ht + 60
      }
    },
    created() {

    },
    methods: {
      handlePointClick(obj){
        this.queryParams.pointId=obj.pointId;
        this.getList();
      },
      /** 查询点位参数关联列表 */
      getList() {
        this.loading = true;
        listPointParam(this.queryParams).then(response => {
          this.pointParamList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          pointId: null,
          paramId: null,
          brand: null,
          model: null,
          isAlarm: null,
          lowValue: null,
          upValue: null,
          avgNum: null,
          avgVal: null,
          paramCodeId: null,
          dpParamId: null,
          slaveId: null,
          funCode: null,
          dataAddr: null,
          dataBit: null,
          storageArea: null,
          storageNo: null,
          dataType: null,
          decode: null,
          dpConversion: null,
          dpRelation: null,
          median: null,
          upRange: null,
          lowRange: null,
          upAvg: null,
          lowAvg: null,
          continuityNum: null,
          dpParamType: null,
          dpParamClass: null,
          isgy: 'N',
          gyx:null,
          gyy:null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加点位参数关联";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPointParam(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改点位参数关联";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePointParam(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPointParam(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除点位参数关联编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPointParam(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有点位参数关联数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPointParam(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
